package com.example.rentsystem.servlet.pages;

import com.example.rentsystem.entity.House;
import com.example.rentsystem.entity.Order;
import com.example.rentsystem.entity.User;
import com.example.rentsystem.service.HouseService;
import com.example.rentsystem.service.OrderService;
import com.example.rentsystem.service.UserService;
import com.example.rentsystem.service.impl.HouseServiceImpl;
import com.example.rentsystem.service.impl.OrderServiceImpl;
import com.example.rentsystem.service.impl.UserServiceImpl;
import com.example.rentsystem.utils.ThymeleafUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.thymeleaf.context.Context;

import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

@WebServlet("/houses")
public class HousesServlet extends HttpServlet {

    HouseService houseService;
    UserService userService;
    OrderService orderService;

    @Override
    public void init() throws ServletException {
        houseService = new HouseServiceImpl();
        userService = new UserServiceImpl();
        orderService = new OrderServiceImpl();
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        User user = (User) req.getSession().getAttribute("user");
        if (user == null) {
            req.getSession().setAttribute("login-first", true);
            resp.sendRedirect("login");
            return;
        }
        Context context = new Context();
        context.setVariable("user", user);
        List<House> houses = houseService.getHouses();
        List<Order> orders = new ArrayList<>();
        houses.forEach(house -> orders.add(orderService.getOrderByHouseId(house.getId())));
        context.setVariable("houses", houses);
        context.setVariable("orders", orders);
        context.setVariable("isAdmin", user.getRole().equals("admin"));
        ThymeleafUtil.process("houses.html", context, resp.getWriter());
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        User user = (User) req.getSession().getAttribute("user");
        if (user == null) {
            req.getSession().setAttribute("login-first", true);
            resp.sendRedirect("login");
            return;
        }
        String keyword = req.getParameter("keyword");
        String location = req.getParameter("location");
        String min = req.getParameter("price_min");
        String max = req.getParameter("price_max");
        boolean a = isNull(keyword), b = isNull(location), c = isNull(min), d = isNull(max);

        List<House> houses = new LinkedList<>();
        houseService.getHouses().forEach(house -> {
            if ((!c && d && house.getPrice() >= Integer.parseInt(min))
                    || (c && !d && house.getPrice() <= Integer.parseInt(max))
                    || (!c && !d && house.getPrice() >= Integer.parseInt(min) && house.getPrice() <= Integer.parseInt(max))
                    || (c && d)) {
                boolean aa = house.getRoom().contains(keyword), bb = house.getLocation().contains(location);
                if (!a && b && aa) houses.add(house);
                else if (a && !b && bb) houses.add(house);
                else if (!a && !b && aa && bb) houses.add(house);
                else if (a && b) houses.add(house);
            }
        });
        List<Order> orders = new ArrayList<>();
        houses.forEach(house -> orders.add(orderService.getOrderByHouseId(house.getId())));
        Context context = new Context();
        context.setVariable("user", req.getSession().getAttribute("user"));
        context.setVariable("houses", houses);
        context.setVariable("orders", orders);
        context.setVariable("isAdmin", user.getRole().equals("admin"));
        ThymeleafUtil.process("houses.html", context, resp.getWriter());
    }

    private boolean isNull(String s) {
        return s == null || s.equals("");
    }
}
